// Copyright (c) 2010 SuccessFactors, Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
//     * Redistributions of source code must retain the above
//       copyright notice, this list of conditions and the following
//       disclaimer.
//
//     * Redistributions in binary form must reproduce the above
//       copyright notice, this list of conditions and the following
//       disclaimer in the documentation and/or other materials
//       provided with the distribution.
//
//     * Neither the name of the SuccessFactors, Inc. nor the names of
//       its contributors may be used to endorse or promote products
//       derived from this software without specific prior written
//       permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
// COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
// OF THE POSSIBILITY OF SUCH DAMAGE.

package org.owasp.jxt.tag.core;

import java.util.ArrayList;
import java.util.List;
import org.owasp.jxt.Attribute;
import org.owasp.jxt.Encoder;
import org.owasp.jxt.JxtTemplate;
import org.owasp.jxt.TranslateContext;

/**
 * CoreTemplateNode
 *
 * @author Jeffrey Ichnowski
 * @version $Revision: 8 $
 */
public final class CoreTemplateNode extends AbstractRootNode {

    private static final String VAR_STACK = CoreTemplateNode.class.getName()+".varStack";

    private String _contextType;

    CoreTemplateNode() {
        super("template");
    }

    @Attribute
    public void setContextType(String type) {
        _contextType = type;
    }

    @Override
    protected void translateClass(TranslateContext ctx) {
        String className = ctx.getTargetClassName();

        String contextType = (_contextType == null ? "Object" : _contextType);

        ctx.codeln("public class "+className+" extends "+
                   JxtTemplate.class.getName()+"<"+
                   contextType+"> {");
        ctx.indent();
        if (true) {
            translateDecl(ctx);
            ctx.codeln();

            ctx.codeln("@Override");
            ctx.codeln("public void render("+contextType+" context, java.io.PrintWriter out) {");
            ctx.indent();
            if (true) {
                translateHeader(ctx);
                translateChildren(ctx, true);
            }
            ctx.outdent();
            ctx.codeln("}");
        }
        ctx.outdent();
        ctx.codeln("}");
    }

    @SuppressWarnings("unchecked")
    public void pushRuntimeEncoder(TranslateContext ctx, Encoder enc) {
        List<String> varStack = (List<String>)ctx.getAttribute(VAR_STACK);
        if (varStack == null) {
            varStack = new ArrayList<String>();
            ctx.setAttribute(VAR_STACK, varStack);
        }

        String varName = ctx.generateVariableName("out");
        varStack.add(varName);

        ctx.codeln("java.io.PrintWriter "+varName+" = out;");
        ctx.codeln("out = new java.io.PrintWriter("+Encoder.class.getName()+"."+enc+".wrap(out));");
        ctx.codeln("try {");
        ctx.indent();
    }

    @SuppressWarnings("unchecked")
    public void popRuntimeEncoder(TranslateContext ctx) {
        List<String> varStack = (List<String>)ctx.getAttribute(VAR_STACK);
        String varName = varStack.remove(varStack.size() - 1);

        ctx.outdent();
        ctx.codeln("} finally {");
        ctx.indent();
        ctx.codeln("out.flush();");
        ctx.codeln("out = "+varName+";");
        ctx.codeln(varName+" = null;");
        ctx.outdent();
        ctx.codeln("}");
    }

} // CoreTemplateNode
